/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Barang.java
 *
 * Created on 30 Jun 11, 22:18:37
 */

package MasterData;

import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import koneksi.koneksi;

/**
 *
 * @author UserXP
 */
public class Barang extends javax.swing.JInternalFrame {

    private DefaultTableModel dtm;
    private JTable table;
    private JTable tabelCari=null;
    koneksi config=new koneksi();

    private boolean inputValid() {
         boolean b;

            if(txtKode_barang.getText().trim().equals("")){
                JOptionPane.showMessageDialog(jDialogBarang, "Kode Barang masih kosong !");
                txtKode_barang.requestFocus();
                b=false;
            }else if(txtNama_barang.getText().trim().equals("")){
                JOptionPane.showMessageDialog(jDialogBarang, "Nama Barang masih kosong !");
                txtNama_barang.requestFocus();
                b=false;
            }else if(txtHarga_beli.getText().trim().equals("")){
                JOptionPane.showMessageDialog(jDialogBarang, "Harga Beli masih kosong !");
                b=false;
            }else if(txtHarga_jual.getText().trim().equals("")){
                JOptionPane.showMessageDialog(jDialogBarang, "Harga jual masih kosong !");
                b=false;
            }else if(txtStok_min.getText().trim().equals("")){
                JOptionPane.showMessageDialog(jDialogBarang, "Stok Minimal masih kosong !");
                b=false;
            }else if(txtStok_max.getText().trim().equals("")){
                JOptionPane.showMessageDialog(jDialogBarang, "Stok Maksimal masih kosong !");
                b=false;
            }else{
             b=true;
            }
              return b;
            }
    private void deleteValid(int row){
        try{
            if(table.getValueAt(row, 4).equals("0")){

            }else{
                JOptionPane.showMessageDialog(this, "Data Tidak dapat dihapus \n " +
                "Stok Ready belum kosong !!!");

            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    private void makeCombo(){
        try{
            Statement st=config.getConnection().createStatement();
            ResultSet rs=st.executeQuery("Select * From golongan");
            jComboGolongan.removeAllItems();
            while(rs.next()){
                jComboGolongan.addItem(rs.getString(2));
            }
            jComboGolongan.setSelectedIndex(0);
            rs.close();
            st.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void tambah(){
        koneksi config=new koneksi();
        config.BuatKoneksiDB();
        try{
            Statement st=config.getConnection().createStatement();
            ResultSet cekkode=st.executeQuery("" +
                    "Select * from barang where kode_barang='"+txtKode_barang.getText()+"'");
            if(cekkode.next()){
                JOptionPane.showMessageDialog(this, "Kode Barang Sudah Digunakan !!");
                txtKode_barang.setText("");
                txtKode_barang.requestFocus();
            }else{
            int tambahdata=st.executeUpdate(
                    "insert into barang  values "+
                    "('"+txtKode_barang.getText()+"','"+txtNama_barang.getText()+"','"
                    +jComboGolongan.getSelectedItem().toString()+"','"
                    +txtHarga_beli.getText()+"','"+txtHarga_jual.getText()+"','"
                    +txtStok_min.getText()+"','"+txtStok_max.getText()+"')");
            int tambahdata_beli=st.executeUpdate(
                    "Insert into pembelian_detil values (null,'1','"+txtKode_barang.getText()+"','"
                    +"0','0')");
            int tambahdata_jual=st.executeUpdate(
                    "Insert into penjualan_detil values (null,'1','"+txtKode_barang.getText()+"','"
                    +"0','0','0')");

            tampilTabel();
            if (tambahdata>0){
                try{
                    JOptionPane.showMessageDialog(this, "Penambahan Data Sukses !!");
                    txtKode_barang.setText("");
                    txtNama_barang.setText("");
                    txtHarga_beli.setText("");
                    txtHarga_jual.setText("");
                    txtStok_min.setText("");
                    txtStok_max.setText("");
                    jDialogBarang.setVisible(false);

                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            }
           st.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Insert Data Gagal");
            e.printStackTrace();
        }


    }
    public void ubah(){
        koneksi config=new koneksi();
        config.BuatKoneksiDB();

        try{
            Statement st=config.getConnection().createStatement();
            int ubahdata=st.executeUpdate("" +
                    "update barang set nama_barang='"+txtNama_barang.getText()+"', " +
                    "golongan='"+jComboGolongan.getSelectedItem().toString()+"',"+
                    "harga_beli='"+txtHarga_beli.getText()+"',"+
                    "harga_jual='"+txtHarga_jual.getText()+"' where kode_barang="+
                    "'"+txtKode_barang.getText()+"'");
            tampilTabel();

            if (ubahdata>0){
                
                jDialogBarang.setVisible(false);
                txtKode_barang.setText("");
                txtNama_barang.setText("");
                txtHarga_beli.setText("");
                txtHarga_jual.setText("");
                txtStok_min.setText("");
                txtStok_max.setText("");
                JOptionPane.showMessageDialog(this," Ubah data Sukses !");
            }
           st.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Update Data Gagal");
        }



    }
    public void hapus(int row){
        koneksi config=new koneksi();
        config.BuatKoneksiDB();

        try{
             if(table.getValueAt(row, 4).equals("0")){
                Statement st=config.getConnection().createStatement();
            int hapusdata=st.executeUpdate("delete from barang where kode_barang='" +
                    ""+txtKode_barang.getText()+"'");
            tampilTabel();
            if (hapusdata>0){
                jDialogBarang.setVisible(false);
                txtKode_barang.setText("");
                txtNama_barang.setText("");
                txtHarga_beli.setText("");
                txtHarga_jual.setText("");
                txtStok_min.setText("");
                txtStok_max.setText("");
                JOptionPane.showMessageDialog(this," Hapus data Sukses !");
            }
           st.close();
            }else{
                jDialogBarang.setVisible(false);
                JOptionPane.showMessageDialog(this, "Data Tidak dapat dihapus \n " +
                "Stok Ready belum kosong !!!");
                
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Hapus Data Gagal");
        }

    }
    private Object[][] getData(){
        Object[][] data1=null;
        try{
            Statement st=config.getConnection().createStatement();
            ResultSet rs=st.executeQuery(
                    "select barang.kode_barang, barang.nama_barang, barang.golongan, barang.harga_jual, sum_stock_in.sum as sum_in, sum_stock_out.sum as sum_out, (sum_stock_in.sum-sum_stock_out.sum) as stok_ready from barang left join (select kode_barang, sum(jumlah_barang) as sum from pembelian_detil group by kode_barang) as sum_stock_in on sum_stock_in.kode_barang = barang.kode_barang left join (select kode_barang, sum(jumlah_barang) as sum from penjualan_detil group by kode_barang) as sum_stock_out on sum_stock_out.kode_barang = barang.kode_barang group by barang.kode_barang");
            rs.last();
            int rowCount=rs.getRow();
            rs.beforeFirst();
            data1=new Object[rowCount][5];
            int no=-1;
            while(rs.next()){
                no=no+1;
                data1[no][0]=rs.getString(1);
                data1[no][1]=rs.getString(2);
                data1[no][2]=rs.getString(3);
                data1[no][3]=rs.getString(4);
                data1[no][4]=rs.getString(7);
            }
            st.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Koneksi SQL Gagal 112");
            e.printStackTrace();
        }
        return data1;
    }
    public void tampilTabel(){
        String[] columnNames={"Kode Barang","Nama","Golongan","Harga Jual","Stok Ready"};
        table=new JTable();
        table.setModel(new DefaultTableModel(getData(),columnNames){
                boolean[] canEdit = new boolean[]{
                    false, false, false, false, false
                };
            @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit[columnIndex];
                }
        });
        jScrollPane1.setViewportView(table);

    }
    private Object[][] getDataCari(){
        Object[][] data1=null;
        try{
            String mcari=txtCari.getText();
            String fieldcari=jComboCari.getSelectedItem().toString();
            Statement st=config.getConnection().createStatement();
            ResultSet rs=st.executeQuery(
                    "select barang.kode_barang, barang.nama_barang, barang.golongan, barang.harga_jual, sum_stock_in.sum as sum_in, sum_stock_out.sum as sum_out, (sum_stock_in.sum-sum_stock_out.sum) as stok_ready from barang left join (select kode_barang, sum(jumlah_barang) as sum from pembelian_detil group by kode_barang) as sum_stock_in on sum_stock_in.kode_barang = barang.kode_barang left join (select kode_barang, sum(jumlah_barang) as sum from penjualan_detil group by kode_barang) as sum_stock_out on sum_stock_out.kode_barang = barang.kode_barang where "+fieldcari+
                    " like ('%"+mcari+"%')group by barang.kode_barang");
            rs.last();
            int rowCount=rs.getRow();
            rs.beforeFirst();
            data1=new Object[rowCount][5];
            int no=-1;
            while(rs.next()){
                no=no+1;
                data1[no][0]=rs.getString(1);
                data1[no][1]=rs.getString(2);
                data1[no][2]=rs.getString(3);
                data1[no][3]=rs.getString(4);
                data1[no][4]=rs.getString(7);


            }

            st.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Koneksi SQL Gagal");
        }
        return data1;
    }
    public void tampilTabelCari(){
       String[] columnNames={"Kode Barang","Nama","Golongan","Harga Jual","Stok Ready"};
        table=new JTable();
        table.setModel(new DefaultTableModel(getDataCari(),columnNames){
                boolean[] canEdit = new boolean[]{
                    false, false, false, false, false
                };
            @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit[columnIndex];
                }
        });
        jScrollPane1.setViewportView(table);
         if(table.getRowCount()==0){
                JOptionPane.showMessageDialog(this,"Data Tidak Ditemukan ");
            }
    }
    private void showData(int row) {
        txtKode_barang.setText(table.getValueAt(row, 0).toString());
        koneksi config=new koneksi();
        config.BuatKoneksiDB();
        int stokread=0;
         try{

                 Statement st=config.getConnection().createStatement();
                 ResultSet rs=st.executeQuery("Select * from barang where" +
                         " kode_barang='"+txtKode_barang.getText()+"'");
             if (rs.next()){
                 txtNama_barang.setText(rs.getString(2));
                 jComboGolongan.setSelectedItem(rs.getString(3));
                 txtHarga_beli.setText(rs.getString(4));
                 txtHarga_jual.setText(rs.getString(5));
                 txtStok_min.setText(rs.getString(6));
                 txtStok_max.setText(rs.getString(7));

              rs.close();
              st.close();
             }
            }catch(Exception e){
                e.printStackTrace();
            }

   
    }
    private void clearTable(){
        table.removeAll();
    }
    /** Creates new form Barang */
    public Barang() {
        initComponents();
        config.BuatKoneksiDB();
        tampilTabel();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialogBarang = new javax.swing.JDialog();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        btnOk = new javax.swing.JButton();
        btnTutupDialog = new javax.swing.JButton();
        jComboGolongan = new javax.swing.JComboBox();
        txtKode_barang = new com.uiMIF.JTextEx();
        txtNama_barang = new com.uiMIF.JTextEx();
        txtHarga_beli = new com.uiMIF.JTextEx();
        txtHarga_jual = new com.uiMIF.JTextEx();
        txtStok_min = new com.uiMIF.JTextEx();
        txtStok_max = new com.uiMIF.JTextEx();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnCari = new javax.swing.JButton();
        jComboCari = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        btnRefresh = new javax.swing.JButton();
        txtCari = new com.uiMIF.JTextEx();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        btnTutup = new javax.swing.JButton();
        btnHapus = new javax.swing.JButton();
        btnUbah = new javax.swing.JButton();
        btnTambah = new javax.swing.JButton();

        jDialogBarang.setTitle("Form Barang");

        jLabel3.setText("Kode Barang");

        jLabel4.setText("Nama Barang");

        jLabel5.setText("Golongan");

        jLabel6.setText("Harga Beli");

        jLabel7.setText("Harga Jual");

        jLabel8.setText("Stok Minimal");

        jLabel9.setText("Stok Maksimal");

        btnOk.setText("OK");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });
        btnOk.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnOkKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                btnOkKeyTyped(evt);
            }
        });

        btnTutupDialog.setText("Tutup");
        btnTutupDialog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTutupDialogActionPerformed(evt);
            }
        });

        jComboGolongan.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jComboGolonganKeyTyped(evt);
            }
        });

        txtKode_barang.setMaxlength(10);
        txtKode_barang.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtKode_barangKeyTyped(evt);
            }
        });

        txtNama_barang.setMaxlength(40);
        txtNama_barang.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNama_barangKeyTyped(evt);
            }
        });

        txtHarga_beli.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtHarga_beli.setInputType(com.uiMIF.JTextEx.TypeText.Number);
        txtHarga_beli.setMaxlength(20);
        txtHarga_beli.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtHarga_beliKeyTyped(evt);
            }
        });

        txtHarga_jual.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtHarga_jual.setInputType(com.uiMIF.JTextEx.TypeText.Number);
        txtHarga_jual.setMaxlength(20);
        txtHarga_jual.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtHarga_jualKeyTyped(evt);
            }
        });

        txtStok_min.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtStok_min.setInputType(com.uiMIF.JTextEx.TypeText.Number);
        txtStok_min.setMaxlength(20);
        txtStok_min.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtStok_minKeyTyped(evt);
            }
        });

        txtStok_max.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtStok_max.setInputType(com.uiMIF.JTextEx.TypeText.Number);
        txtStok_max.setMaxlength(20);
        txtStok_max.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtStok_maxKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jDialogBarangLayout = new javax.swing.GroupLayout(jDialogBarang.getContentPane());
        jDialogBarang.getContentPane().setLayout(jDialogBarangLayout);
        jDialogBarangLayout.setHorizontalGroup(
            jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogBarangLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addGap(23, 23, 23)
                .addGroup(jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtStok_max, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtStok_min, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtHarga_jual, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtHarga_beli, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jDialogBarangLayout.createSequentialGroup()
                        .addComponent(btnOk)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnTutupDialog))
                    .addComponent(jComboGolongan, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtKode_barang, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNama_barang, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        jDialogBarangLayout.setVerticalGroup(
            jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogBarangLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtKode_barang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtNama_barang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jComboGolongan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtHarga_beli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtHarga_jual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtStok_min, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtStok_max, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialogBarangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnTutupDialog)
                    .addComponent(btnOk))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        setTitle("Form Barang");

        jLabel1.setText("Masukkan Kata Kunci");

        btnCari.setText("Cari");
        btnCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariActionPerformed(evt);
            }
        });
        btnCari.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                btnCariKeyTyped(evt);
            }
        });

        jComboCari.setMaximumRowCount(2);
        jComboCari.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "nama_barang", "kode_barang" }));
        jComboCari.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jComboCariKeyTyped(evt);
            }
        });

        jLabel2.setText("Pilih Jenis Kunci");

        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        txtCari.setMaxlength(15);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(txtCari, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jComboCari, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCari)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnRefresh))
                    .addComponent(jLabel2))
                .addContainerGap(79, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCari)
                    .addComponent(btnRefresh)
                    .addComponent(txtCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        jPanel2.setLayout(new java.awt.BorderLayout());
        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        btnTutup.setText("Tutup ");
        btnTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTutupActionPerformed(evt);
            }
        });

        btnHapus.setText("Hapus ");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });

        btnUbah.setText(" Ubah ");
        btnUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUbahActionPerformed(evt);
            }
        });

        btnTambah.setText("Tambah");
        btnTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTambahActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(210, Short.MAX_VALUE)
                .addComponent(btnTambah)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnUbah)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnHapus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnTutup)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTutup)
                    .addComponent(btnHapus)
                    .addComponent(btnUbah)
                    .addComponent(btnTambah))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTutupActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnTutupActionPerformed

    private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTambahActionPerformed
        // TODO add your handling code here:
        txtKode_barang.setText("");
        txtNama_barang.setText("");
        txtHarga_beli.setText("");
        txtHarga_jual.setText("");
        txtStok_min.setText("");
        txtStok_max.setText("");
        txtKode_barang.setEditable(true);
        txtNama_barang.setEditable(true);
        jComboGolongan.setEnabled(true);
        txtHarga_beli.setEditable(true);
        txtHarga_jual.setEditable(true);
        txtStok_min.setEditable(true);
        txtStok_max.setEditable(true);
        jDialogBarang.setSize(300, 320);
        jDialogBarang.setLocationRelativeTo(null);
        jDialogBarang.setTitle(" Tambah Data Barang");
        btnOk.setText("Tambah");
        txtKode_barang.setEditable(true);
        makeCombo();
        txtKode_barang.requestFocus();
        jDialogBarang.setVisible(true);
    }//GEN-LAST:event_btnTambahActionPerformed

    private void btnUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUbahActionPerformed
        // TODO add your handling code here:
         if(table.getSelectedRow()<0){

            JOptionPane.showMessageDialog(null, "Silahkan Pilih Data yang akan dirubah");
        }else{
        makeCombo();
        showData(table.getSelectedRow());
        jDialogBarang.setSize(300, 320);
        jDialogBarang.setLocationRelativeTo(null);
        jDialogBarang.setTitle(" Ubah Data Barang");
        btnOk.setText("Ubah");
        txtKode_barang.setEditable(false);
        txtNama_barang.setEditable(true);
        jComboGolongan.setEnabled(true);
        txtHarga_beli.setEditable(true);
        txtHarga_jual.setEditable(true);
        txtStok_min.setEditable(true);
        txtStok_max.setEditable(true);
        txtNama_barang.requestFocus();
        jDialogBarang.setVisible(true);
        txtNama_barang.requestFocus();
       
        }
       
    }//GEN-LAST:event_btnUbahActionPerformed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        // TODO add your handling code here:
        if(btnOk.getText().equals("Tambah")){
            if(inputValid()){
               tambah();
                }
        }else if(btnOk.getText().equals("Ubah")){
            if(inputValid()){
                ubah();
                }
        }else if(btnOk.getText().equals("Hapus")){
            hapus(table.getSelectedRow());
        }
    }//GEN-LAST:event_btnOkActionPerformed

    private void btnTutupDialogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTutupDialogActionPerformed
        // TODO add your handling code here:
        txtKode_barang.setText("");
        txtNama_barang.setText("");
        txtHarga_beli.setText("");
        txtHarga_jual.setText("");
        txtStok_min.setText("");
        txtStok_max.setText("");
        jDialogBarang.setVisible(false);
        txtCari.requestFocus();
    }//GEN-LAST:event_btnTutupDialogActionPerformed

    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
        // TODO add your handling code here:.
        if(table.getSelectedRow()<0){
            JOptionPane.showMessageDialog(null, "Silahkan Pilh Data yang akan dihapus");
        }else{
        makeCombo();
        showData(table.getSelectedRow());
        jDialogBarang.setSize(300, 320);
        jDialogBarang.setLocationRelativeTo(null);
        jDialogBarang.setTitle("Hapus Data");
        btnOk.setText("Hapus");
        txtKode_barang.setEditable(false);
        txtNama_barang.setEditable(false);
        jComboGolongan.setEnabled(false);
        txtHarga_beli.setEditable(false);
        txtHarga_jual.setEditable(false);
        txtStok_min.setEditable(false);
        txtStok_max.setEditable(false);
        jDialogBarang.setVisible(true);

        }
    }//GEN-LAST:event_btnHapusActionPerformed

    private void btnCari(){
        if(txtCari.getText().equals("")){
            JOptionPane.showMessageDialog(this, "Silahkan Masukkan Kata Kunci !");
            txtCari.requestFocus();
        }else{
        tampilTabelCari();
        }
    }
    private void btnCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariActionPerformed
        // TODO add your handling code here:
        btnCari();
    }//GEN-LAST:event_btnCariActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        // TODO add your handling code here:
        tampilTabel();
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void txtKode_barangKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtKode_barangKeyTyped
        // TODO add your handling code here:
        if(evt.getKeyChar()==evt.VK_ENTER)
            txtNama_barang.requestFocus();
    }//GEN-LAST:event_txtKode_barangKeyTyped

    private void txtNama_barangKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNama_barangKeyTyped
        // TODO add your handling code here:
        if(evt.getKeyChar()==evt.VK_ENTER)
            jComboGolongan.requestFocus();
    }//GEN-LAST:event_txtNama_barangKeyTyped

    private void jComboGolonganKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jComboGolonganKeyTyped
        // TODO add your handling code here:
        if(evt.getKeyChar()==evt.VK_ENTER)
            txtHarga_beli.requestFocus();
    }//GEN-LAST:event_jComboGolonganKeyTyped

    private void txtHarga_beliKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtHarga_beliKeyTyped
        // TODO add your handling code here:
        if(evt.getKeyChar()==evt.VK_ENTER)
            txtHarga_jual.requestFocus();
    }//GEN-LAST:event_txtHarga_beliKeyTyped

    private void txtHarga_jualKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtHarga_jualKeyTyped
        // TODO add your handling code here:
        if(evt.getKeyChar()==evt.VK_ENTER)
            txtStok_min.requestFocus();
    }//GEN-LAST:event_txtHarga_jualKeyTyped

    private void txtStok_minKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtStok_minKeyTyped
        // TODO add your handling code here:
        if(evt.getKeyChar()==evt.VK_ENTER)
            txtStok_max.requestFocus();
    }//GEN-LAST:event_txtStok_minKeyTyped

    private void txtStok_maxKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtStok_maxKeyTyped
        // TODO add your handling code here:
        if(evt.getKeyChar()==evt.VK_ENTER)
            btnOk.requestFocus();
    }//GEN-LAST:event_txtStok_maxKeyTyped

    private void btnOkKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnOkKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnOkKeyPressed

    private void btnOkKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnOkKeyTyped
        // TODO add your handling code here:
        if(evt.getKeyChar()==evt.VK_ENTER){
            if(btnOk.getText().equals("Tambah")){
            if(inputValid()){
               tambah();
                }
           }else if(btnOk.getText().equals("Ubah")){
            if(inputValid()){
                ubah();
                }
           }else if(btnOk.getText().equals("Hapus")){
            hapus(table.getSelectedRow());
           }
        }

    }//GEN-LAST:event_btnOkKeyTyped

    private void jComboCariKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jComboCariKeyTyped
        // TODO add your handling code here:
        if(evt.getKeyChar()==evt.VK_ENTER)
            btnCari.requestFocus();
    }//GEN-LAST:event_jComboCariKeyTyped

    private void btnCariKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnCariKeyTyped
        // TODO add your handling code here:
        btnCari();
    }//GEN-LAST:event_btnCariKeyTyped


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCari;
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnOk;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnTambah;
    private javax.swing.JButton btnTutup;
    private javax.swing.JButton btnTutupDialog;
    private javax.swing.JButton btnUbah;
    private javax.swing.JComboBox jComboCari;
    private javax.swing.JComboBox jComboGolongan;
    private javax.swing.JDialog jDialogBarang;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private com.uiMIF.JTextEx txtCari;
    private com.uiMIF.JTextEx txtHarga_beli;
    private com.uiMIF.JTextEx txtHarga_jual;
    private com.uiMIF.JTextEx txtKode_barang;
    private com.uiMIF.JTextEx txtNama_barang;
    private com.uiMIF.JTextEx txtStok_max;
    private com.uiMIF.JTextEx txtStok_min;
    // End of variables declaration//GEN-END:variables

}
